package biz;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

import dao.*;

public class DdComBiz {
	Configuration configuration = null;
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction transaction = null;
	
	public void AddMetalPer(TDdMetalper metal){
		ModMetalPer(metal);
	}
	
	public void ModMetalPer(TDdMetalper metal){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(metal);
		transaction.commit();
		session.close();
	}
	
	public void DelMetalPer(TDdMetalper metal){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdMetalper tddmp = (TDdMetalper)session.get(TDdMetalper.class, metal.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetMetalPer(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdMetalper as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetMetalPer(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdMetalper as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddJewType(TDdJewtype jtype){
		ModJewType(jtype);
	}
	
	public void ModJewType(TDdJewtype jtype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(jtype);
		transaction.commit();
		session.close();
	}
	
	public void DelJewType(TDdJewtype jtype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdJewtype tddmp = (TDdJewtype)session.get(TDdJewtype.class, jtype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetJewType(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdJewtype as f  order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetJewType(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdJewtype as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddSsType(TDdSstype stype){
		ModSsType(stype);
	}
	
	public void ModSsType(TDdSstype stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
	}
	
	public void DelSsType(TDdSstype stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdSstype tddmp = (TDdSstype)session.get(TDdSstype.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetSsType(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdSstype as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetSsType(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdSstype as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddShape(TDdShape stype){
		ModShape(stype);
	}
	
	public void ModShape(TDdShape stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
	}
	
	public void DelShape(TDdShape stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdShape tddmp = (TDdShape)session.get(TDdShape.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetShape(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdShape as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetShape(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdShape as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddColor(TDdColor stype){
		ModColor(stype);
	}
	
	public void ModColor(TDdColor stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
	}
	
	public void DelColor(TDdColor stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();		
		transaction = session.beginTransaction();
		TDdColor tddmp = (TDdColor)session.get(TDdColor.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetColor(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdColor as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetColor(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdColor as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddClear(TDdClear stype){
		ModClear(stype);
	}
	
	public void ModClear(TDdClear stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	public void DelClear(TDdClear stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdClear tddmp = (TDdClear)session.get(TDdClear.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetClear(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdClear as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetClear(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdClear as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddLathe(TDdLathe stype){
		ModLathe(stype);
	}
	
	public void ModLathe(TDdLathe stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
	}
	
	public void DelLathe(TDdLathe stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdLathe tddmp = (TDdLathe)session.get(TDdLathe.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetLathe(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdLathe as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetLathe(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdLathe as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddEmbed(TDdEmbed stype){
		ModEmbed(stype);
	}
	
	public void ModEmbed(TDdEmbed stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
	}
	
	public void DelEmbed(TDdEmbed stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdEmbed tddmp = (TDdEmbed)session.get(TDdEmbed.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetEmbed(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdEmbed as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetEmbed(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdEmbed as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddSpec(TDdSpec stype){
		ModSpec(stype);
	}
	
	public void ModSpec(TDdSpec stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
	}
	
	public void DelSpec(TDdSpec stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TDdSpec tddmp = (TDdSpec)session.get(TDdSpec.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetSpec(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdSpec as f order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetSpec(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TDdSpec as f where f.id =" + oid + " order by f.id");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public void AddGoldPrice(TGoldPrice stype){
		ModGoldPrice(stype);
	}
	
	public void ModGoldPrice(TGoldPrice stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(stype);
		transaction.commit();
		session.close();
	}
	
	public void DelGoldPrice(TGoldPrice stype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TGoldPrice tddmp = (TGoldPrice)session.get(TGoldPrice.class, stype.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetGoldPrice(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TGoldPrice as f order by f.id desc");
		
		List<TGoldPrice> list = query.list();
		session.close();
		return list;
	}
	
	public Object GetGoldPrice(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TGoldPrice as f where f.id =" + oid + " order by f.id desc");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DdComBiz dd = new DdComBiz();
		System.out.println(dd.GetJewType());

	}

}
